package org.loofer.uikit;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;

public class ProgressLayout extends RelativeLayout {
    private static final String ERROR_TAG = "ProgressLayout.error_tag";
    private static final String LOADING_TAG = "ProgressLayout.loading_tag";
    private static final String NONE_TAG = "ProgressLayout.none_tag";
    private static final int NOT_SET = -1;
    private static final int defStyleAttr = R.attr.progressLayoutDefStyle;
    private List<View> contentViews;
    private LAYOUT_TYPE currentState;
    private View failedContainer;
    private int failedId;
    private LayoutInflater layoutInflater;
    private View loadingContainer;
    private int loadingId;
    private View networkErrorContainer;
    private int networkErrorId;
    private View noneContainer;
    private int noneId;

    public enum LAYOUT_TYPE {
        LOADING,
        NONE,
        CONTENT,
        NETWORK_ERROR,
        FAILED
    }

    public ProgressLayout(Context context) {
        this(context, null);
    }

    public ProgressLayout(Context context, AttributeSet attrs) {
        this(context, attrs, defStyleAttr);
    }

    public ProgressLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.contentViews = new ArrayList();
        this.currentState = LAYOUT_TYPE.LOADING;
        if (!isInEditMode()) {
            init(context, attrs, defStyleAttr);
        }
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.layoutInflater = (LayoutInflater) context.getSystemService("layout_inflater");
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressLayout, defStyleAttr, R.style.DefaultSmartStyle);
        if (typedArray != null) {
            try {
                this.loadingId = typedArray.getResourceId(R.styleable.ProgressLayout_loading_layout, -1);
                this.noneId = typedArray.getResourceId(R.styleable.ProgressLayout_none_content, -1);
                this.networkErrorId = typedArray.getResourceId(R.styleable.ProgressLayout_network_content, -1);
                this.failedId = typedArray.getResourceId(R.styleable.ProgressLayout_failed_content, -1);
            } finally {
                typedArray.recycle();
            }
        }
    }

    public void addView(View child, int index, LayoutParams params) {
        super.addView(child, index, params);
        if (child.getTag() == null || !(child.getTag().equals(LOADING_TAG) || child.getTag().equals(NONE_TAG) || child.getTag().equals(ERROR_TAG))) {
            this.contentViews.add(child);
            if (!isInEditMode()) {
                setContentVisibility(false);
            }
        }
    }

    public void showLoading() {
        showLoadingView();
        hideNoneView();
        hideNetErrorView();
        hideFailedView();
        setContentVisibility(false);
    }

    public void showNone() {
        showNone(null);
    }

    public void showNone(OnClickListener retryListener) {
        showNoneView(retryListener);
        hideLoadingView();
        hideNetErrorView();
        hideFailedView();
        setContentVisibility(false);
    }

    public void showNetError() {
        showNetError(null);
    }

    public void showNetError(OnClickListener retryListener) {
        showNetErrorView(retryListener);
        hideLoadingView();
        hideNoneView();
        hideFailedView();
        setContentVisibility(false);
    }

    public void showFailed() {
        showFailed(null);
    }

    public void showFailed(OnClickListener retryListener) {
        showFailedView(retryListener);
        hideLoadingView();
        hideNoneView();
        hideNetErrorView();
        setContentVisibility(false);
    }

    public void showContent() {
        hideLoadingView();
        hideNoneView();
        hideNetErrorView();
        hideFailedView();
        setContentVisibility(true);
    }

    public LAYOUT_TYPE getCurrentState() {
        return this.currentState;
    }

    private void showLoadingView() {
        if (this.loadingContainer != null) {
            this.loadingContainer.setVisibility(0);
        } else if (this.loadingId == -1) {
            throw new IllegalStateException("cannot call showLoadingView() when loadingId was NO_SET which value is -1");
        } else {
            this.loadingContainer = this.layoutInflater.inflate(this.loadingId, this, false);
            this.loadingContainer.setTag(LOADING_TAG);
            LayoutParams layoutParams = (LayoutParams) this.loadingContainer.getLayoutParams();
            layoutParams.addRule(13);
            addView(this.loadingContainer, layoutParams);
        }
    }

    private void showNoneView(OnClickListener retryListener) {
        if (this.noneContainer != null) {
            this.noneContainer.setVisibility(0);
        } else if (this.noneId == -1) {
            throw new IllegalStateException("cannot call showNoneView() when noneId was NO_SET which value is -1");
        } else {
            this.noneContainer = this.layoutInflater.inflate(this.noneId, this, false);
            this.noneContainer.setTag(NONE_TAG);
            LayoutParams layoutParams = (LayoutParams) this.noneContainer.getLayoutParams();
            layoutParams.addRule(13);
            addView(this.noneContainer, layoutParams);
            if (retryListener != null) {
                this.noneContainer.setClickable(true);
                this.noneContainer.setOnClickListener(retryListener);
            }
        }
    }

    private void showNetErrorView(OnClickListener retryListener) {
        if (this.networkErrorContainer != null) {
            this.networkErrorContainer.setVisibility(0);
        } else if (this.networkErrorId == -1) {
            throw new IllegalStateException("cannot call showNetErrorView() when networkErrorId was NO_SET which value is -1");
        } else {
            this.networkErrorContainer = this.layoutInflater.inflate(this.networkErrorId, this, false);
            this.networkErrorContainer.setTag(ERROR_TAG);
            LayoutParams layoutParams = (LayoutParams) this.networkErrorContainer.getLayoutParams();
            layoutParams.addRule(13);
            addView(this.networkErrorContainer, layoutParams);
            if (retryListener != null) {
                this.networkErrorContainer.setClickable(true);
                this.networkErrorContainer.setOnClickListener(retryListener);
            }
        }
    }

    private void showFailedView(OnClickListener retryListener) {
        if (this.failedContainer != null) {
            this.failedContainer.setVisibility(0);
        } else if (this.failedId == -1) {
            throw new IllegalStateException("cannot call showFailedView() when failedId was NO_SET which value is -1");
        } else {
            this.failedContainer = this.layoutInflater.inflate(this.failedId, this, false);
            this.failedContainer.setTag(ERROR_TAG);
            LayoutParams layoutParams = (LayoutParams) this.failedContainer.getLayoutParams();
            layoutParams.addRule(13);
            addView(this.failedContainer, layoutParams);
            if (retryListener != null) {
                this.failedContainer.setClickable(true);
                this.failedContainer.setOnClickListener(retryListener);
            }
        }
    }

    private void hideLoadingView() {
        if (this.loadingContainer != null && this.loadingContainer.getVisibility() != 8) {
            this.loadingContainer.setVisibility(8);
        }
    }

    private void hideNoneView() {
        if (this.noneContainer != null && this.noneContainer.getVisibility() != 8) {
            this.noneContainer.setVisibility(8);
        }
    }

    private void hideNetErrorView() {
        if (this.networkErrorContainer != null && this.networkErrorContainer.getVisibility() != 8) {
            this.networkErrorContainer.setVisibility(8);
        }
    }

    private void hideFailedView() {
        if (this.failedContainer != null && this.failedContainer.getVisibility() != 8) {
            this.failedContainer.setVisibility(8);
        }
    }

    public boolean isLoading() {
        return this.currentState == LAYOUT_TYPE.LOADING;
    }

    public boolean isContent() {
        return this.currentState == LAYOUT_TYPE.CONTENT;
    }

    public boolean isNone() {
        return this.currentState == LAYOUT_TYPE.NONE;
    }

    public boolean isNetworkError() {
        return this.currentState == LAYOUT_TYPE.NETWORK_ERROR;
    }

    public boolean isFailed() {
        return this.currentState == LAYOUT_TYPE.FAILED;
    }

    private void setContentVisibility(boolean visible) {
        for (View contentView : this.contentViews) {
            contentView.setVisibility(visible ? 0 : 8);
        }
    }
}
